package com.ruoyi.jkcrData.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.calculate.BezierCurveUtil;
import com.ruoyi.jkcrData.domain.plat.DataJkcrZhchxx;
import com.ruoyi.jkcrData.domain.plat.JkcrZhchxxChange;
import com.ruoyi.jkcrData.mapper.DataJkcrZhchxxMapper;
import com.ruoyi.jkcrData.service.IDataJkcrZhchxxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;


/**
 * 架空乘人重车下行空车上行测试数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-19
 */
@Service
public class DataJkcrZhchxxServiceImpl implements IDataJkcrZhchxxService
{
    @Autowired(required = false)
    private DataJkcrZhchxxMapper dataJkcrZhchxxMapper;

    /**
     * 查询架空乘人重车下行空车上行测试数据
     * 
     * @param zhchxxId 架空乘人重车下行空车上行测试数据主键
     * @return 架空乘人重车下行空车上行测试数据
     */
    @Override
    public DataJkcrZhchxx selectDataJkcrZhchxxByZhchxxId(Long zhchxxId)
    {
        return dataJkcrZhchxxMapper.selectDataJkcrZhchxxByZhchxxId(zhchxxId);
    }

    /**
     * 根据dataTaskId获取数据
     *
     * @param dataTaskId
     */
    @Override
    public Future<List<DataJkcrZhchxx>> selectDataJkcrZhchxxByDataTaskId(Long dataTaskId) {
        List<DataJkcrZhchxx> dataJkcrZhchxxxEntities = dataJkcrZhchxxMapper.selectDataJkcrZhchxxByDataTaskId(dataTaskId);
        if (!dataJkcrZhchxxxEntities.isEmpty()) {
            dataJkcrZhchxxxEntities.forEach(item -> {
                if (item.getSdList() != null) {
                    // 速度曲线数据
                    List<Float> suduVelList = JSON.parseArray(item.getSdList(), Float.class);
                    float stepX = Float.parseFloat(item.getZdshj()) / suduVelList.size();
                    item.setSdChartLineDTO(BezierCurveUtil.calPoints(suduVelList, stepX, true));
                }
            });
        }
        return new AsyncResult<>(dataJkcrZhchxxxEntities);
    }

    /**
     * 查询架空乘人重车下行空车上行测试数据列表
     * 
     * @param dataJkcrZhchxx 架空乘人重车下行空车上行测试数据
     * @return 架空乘人重车下行空车上行测试数据
     */
    @Override
    public List<DataJkcrZhchxx> selectDataJkcrZhchxxList(DataJkcrZhchxx dataJkcrZhchxx)
    {
        return dataJkcrZhchxxMapper.selectDataJkcrZhchxxList(dataJkcrZhchxx);
    }

    /**
     * 新增架空乘人重车下行空车上行测试数据
     * 
     * @param dataJkcrZhchxx 架空乘人重车下行空车上行测试数据
     * @return 结果
     */
    @Override
    public int insertDataJkcrZhchxx(DataJkcrZhchxx dataJkcrZhchxx)
    {
        dataJkcrZhchxx.setCreateTime(DateUtils.getNowDate());
        return dataJkcrZhchxxMapper.insertDataJkcrZhchxx(dataJkcrZhchxx);
    }

    /**
     * 批量新增架空乘人重车下行空车上行测试数据
     *
     * @param dataJkcrZhchxx
     */
    @Override
    public int batchDataJkcrZhchxx(List<DataJkcrZhchxx> dataJkcrZhchxx) {
        return dataJkcrZhchxxMapper.batchDataJkcrZhchxx(dataJkcrZhchxx);
    }

    /**
     * 修改架空乘人重车下行空车上行测试数据
     * 
     * @param dataJkcrZhchxx 架空乘人重车下行空车上行测试数据
     * @return 结果
     */
    @Override
    public int updateDataJkcrZhchxx(DataJkcrZhchxx dataJkcrZhchxx)
    {
        dataJkcrZhchxx.setUpdateTime(DateUtils.getNowDate());
        return dataJkcrZhchxxMapper.updateDataJkcrZhchxx(dataJkcrZhchxx);
    }

    /**
     * 修改架空乘人重车下行空车上行测试数据
     *
     * @param jkcrZhchxxChange@return 结果
     */
    @Override
    public DataJkcrZhchxx modifyDataJkcrZhchxx(JkcrZhchxxChange jkcrZhchxxChange) {
        DataJkcrZhchxx dataJkcrZhchxx = dataJkcrZhchxxMapper.selectDataJkcrZhchxxByZhchxxId(jkcrZhchxxChange.getZhchxxId());
        List<Float> suduVelList = JSON.parseArray(dataJkcrZhchxx.getSdList(), Float.class);
        float zhdchsdOld = suduVelList.get(0);
        float zhdshjOld = suduVelList.size() * 0.1f;
        zhdchsdOld = zhdchsdOld == 0 ? 1 : zhdchsdOld;
        zhdshjOld = zhdshjOld == 0 ? 1 : zhdshjOld;
        dataJkcrZhchxx.setZdshj(jkcrZhchxxChange.getZdshj());
        float addX = 0f;
        float xishu = Float.parseFloat(jkcrZhchxxChange.getZhdchsd()) / zhdchsdOld;
        float shjXishu = Float.parseFloat(jkcrZhchxxChange.getZdshj()) / zhdshjOld;
        float stepX = zhdshjOld / suduVelList.size() * shjXishu;
        List<Float> sdList = new ArrayList<>();
        List<Float> xVelList = new ArrayList<>(); List<Float> yVelList = new ArrayList<>();
        for (Float sdVel : suduVelList) {
            sdVel = sdVel * xishu;
            addX += stepX;
            xVelList.add(addX);
            yVelList.add(sdVel);
            sdList.add(sdVel);
        }
        dataJkcrZhchxx.setSdChartLineDTO(BezierCurveUtil.calPoints(xVelList, yVelList, true));
        dataJkcrZhchxx.setSdList(JSON.toJSONString(sdList));
        float zhdchsdNew = yVelList.get(0);
        dataJkcrZhchxx.setZhdjsd(String.format("%.2f", (zhdchsdNew / Float.parseFloat(dataJkcrZhchxx.getZdshj()))));
        dataJkcrZhchxx.setZhdjl(String.format("%.2f",(Float.parseFloat(dataJkcrZhchxx.getZhdjsd())
                * Float.parseFloat(dataJkcrZhchxx.getZdshj()) * Float.parseFloat(dataJkcrZhchxx.getZdshj()) * 0.5f)));
        updateDataJkcrZhchxx(dataJkcrZhchxx);
        return dataJkcrZhchxx;
    }

    /**
     * 变更数据状态
     *
     * @param dataTaskId
     * @return 结果
     */
    @Override
    public int updateDataJkcrZhchxxByDataTaskId(Long dataTaskId) {
        return dataJkcrZhchxxMapper.updateDataJkcrZhchxxByDataTaskId(dataTaskId);
    }

    /**
     * 变更数据状态
     *
     * @param zhchxxId
     * @return 结果
     */
    @Override
    public int updateDataJkcrZhchxxByZhchxxId(Long zhchxxId) {
        return dataJkcrZhchxxMapper.updateDataJkcrZhchxxByZhchxxId(zhchxxId);
    }

    /**
     * 批量删除架空乘人重车下行空车上行测试数据
     * 
     * @param zhchxxIds 需要删除的架空乘人重车下行空车上行测试数据主键
     * @return 结果
     */
    @Override
    public int deleteDataJkcrZhchxxByZhchxxIds(Long[] zhchxxIds)
    {
        return dataJkcrZhchxxMapper.deleteDataJkcrZhchxxByZhchxxIds(zhchxxIds);
    }

    /**
     * 删除架空乘人重车下行空车上行测试数据信息
     * 
     * @param zhchxxId 架空乘人重车下行空车上行测试数据主键
     * @return 结果
     */
    @Override
    public int deleteDataJkcrZhchxxByZhchxxId(Long zhchxxId)
    {
        return dataJkcrZhchxxMapper.deleteDataJkcrZhchxxByZhchxxId(zhchxxId);
    }
}
